/**
 * @description: Using a single array to implement three stacks.
 * @author: timlentse(tinglenxan@gmail.com)
 * @class name: ArrayTo3Stack
 * @Date: 2014-11-04
 */

public class ArrayTo3Stack {
	private static int	stackSize	= 100;
	private static int[]	buffer		= new int [stackSize * 3];
	private static int[]	stackPointer	= { 0, 0, 0 }; /* stack pointers to track top element */

	public static void main( String[] args )
	{
		for ( int i = 0; i < 10; ++i )
		{
			push( 0, i );
			push( 1, i + 1 );
			push( 2, i + 2 );
		}
		for ( int i = 0; i < 10; ++i )
		{
			System.out.println( pop( 0 ) + " " + pop( 1 ) + " " + pop( 2 ) + "\n" );
		}
	}


	private static int  push( int stackNum, int value )
	{
/* Find the index of the top element in the array + 1, and
 * increase the stack pointer */
		if ( !isFull( stackNum ) )
		{
			int index = stackNum * stackSize + stackPointer[stackNum]++;
			buffer[index] = value;
			return(0);
		}else
			return(-1);
	}


	private static int pop( int stackNum )
	{
		if ( !isEmpty( stackNum ) )
		{
			int	index	= stackNum * stackSize + --stackPointer[stackNum];
			int	value	= buffer[index];
			buffer[index] = 0;
			return(value);
		} else
			return(-1);
	}


	private static int peek( int stackNum )
	{
		int index = stackNum * stackSize + --stackPointer[stackNum];
		return(buffer[index]);
	}


	private static boolean isFull( int stackNum )
	{
		return(stackPointer[stackNum] == (stackNum + 1) * stackSize);
	}


	private static boolean isEmpty( int stackNum )
	{
		return(stackPointer[stackNum] == stackNum * stackSize);
	}
}